<?php
class User extends AppModel {
	public $name = 'User';
	public $displayField = 'name';
	public $actsAs = array('Acl' => 'requester');
	public $validate = array(
		'name' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				
			),
		),
		'username' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				
			),
		),
		'email' => array(
			'email' => array(
				'rule' => array('email'),
			),
		),
		'password' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				
			),
		),
	);

/**
 * Config has many relationship
 * 
 *
 * @var array
 */
	public  $hasMany = array(
		'Friend' => array(
			'className' => 'Friend',
			'foreignKey' => 'user_id',
		),
		'Notice' => array(
			'className' => 'Notice',
			'foreignKey' => 'user_id',
		),
		'UserStatus' => array(
			'className' => 'UserStatus',
			'foreignKey' => 'user_id'
		),
		'Avatar' => array(
			'className' => 'Avatar',
			'foreignKey' => 'user_id'
		),
		'Company' => array(
			'className' => 'Company',
			'foreignKey' => 'user_id'
		),
                'UserMenu' => array(
                    'className' => 'UserMenu',
                    'foreignKey' => 'user_id'
                ),
                'Like' => array(
                    'className' => 'Like',
                    'foreignKey' => 'user_id'
                )
	);
	

	public $belongsTo = array(
		'Group' => array(
			'className' => 'Group',
			'foreignKey' => 'group_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

/**
 * Create note acls
 *
 * @return array of Group or null
 */
	 function parentNode() {
        if (!$this->id && empty($this->data)) {
            return null;
        }
        $data = $this->data;
        if (empty($this->data)) {
            $data = $this->read();
        }
        if (!isset($data['User']['group_id'])) {
            return null;
        } else {
            return array('Group' => array('id' => $data['User']['group_id']));
        }
    }
/**
 * After save callback
 *
 * Update the aro for the user.
 *
 * @access public
 * @return void
 */
    function afterSave($created) {
            if (!$created) {
                $parent = $this->parentNode();
                $parent = $this->node($parent);
                $node = $this->node();
                $aro = $node[0];
                $aro['Aro']['parent_id'] = $parent[0]['Aro']['id'];
                $this->Aro->save($aro);
            }
    }
    
    public function getUsername($userId){
    	
    	$username = $this->find('first', array('conditions' => array('User.id' => $userId)));
    	if( !empty($username) )
    		return $username['User']['username'];
    	else 
    		return false;	
    }
    
    public function checkField($fieldname, $value){
    	
    	$count = $this->find('count', array('conditions' => array('User.'.$fieldname => $value), 'recursive' => -1));
    	if($count == 1){
    		return true;
    	}else{
    		return false;
    	}
    }

    public function getUser($userId){
        $user = $this->find('first', array('conditions' => array('User.id' => $userId), 'recursive' => -1));
        if($user)
            return $user;
        else
            return false;
    }
}
?>